package com.dat255.tentatime;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import com.dat255.internalstorage.*;
import android.view.MenuItem;
import android.widget.TextView;

/**
 * This activity is used for displaying all information 
 * about an exam. It must receive an exam from previous
 * activity
 * @author Joseph
 *
 */
public class ShowExamActivity extends Activity{

	private TextView courseId, courseName, coursePlace, courseTime, courseDate;
	private Exam exam;
	private boolean flag;
	private FavoritesManagement favmanage = new FavoritesManagement(this,15);
	
	//TODO: make onOptionsItemSelected better, put comment
	 /** 
     * Called when the activity is first created. 
     * This method will recieve an exam object from the previous activity
     * and display all the details from the exam object
     */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
			super.onCreate(savedInstanceState);
			
			//Set the layout for this activity
			setContentView(R.layout.exam); 
			//Make the GUI components editable
			this.attachGUIcomponents();
			//Get the exam object from previous activity
			this.getExam();
			//Read the exam object and display the information
			this.printExam();
		
	}
	
	/*****************************************************/
	/************** PRIVATE METHODS **********************/
	/*****************************************************/
	
	 /**
     * The internal method will "connect" the input/output components
     * of the GUI for this activity for use in this code
     */
    private void attachGUIcomponents(){
    	
    	 courseId = (TextView) findViewById(R.id.textVCourseID);
		 courseName = (TextView) findViewById(R.id.textVcourseName);
		 coursePlace = (TextView) findViewById(R.id.textVPlace);
		 courseTime = (TextView) findViewById(R.id.textVTime);
		 courseDate = (TextView) findViewById(R.id.textVDate);
    	
    }
    
    /**
     * This method will retrieve the examobject passed from the previous activity
     */
    private void getExam(){
		 Bundle data = getIntent().getExtras();
		 //Get the exam from previos activity
		  exam = data.getParcelable(ShowExamActivity.this.getString(R.string.keySelectedExam));  
		  // The flag is used to determine if the previous activity was an resultactivity or favourite activity
		  flag = data.getBoolean(ShowExamActivity.this.getString(R.string.keyMenuEnable));
    	
    }
    
    /**
     * Display all the details about an exam 
     */
    private void printExam(){
    	
   	 courseId.setText(exam.getCourseID());
	 courseName.setText(exam.getCourseName());
	 coursePlace.setText(exam.getPlace());
	 courseTime.setText(exam.getTime());
	 courseDate.setText(exam.getDate());
    	
    	
    	
    }
	
	/*****************************************************/
	/******************** MENU ***************************/
	/*****************************************************/
	
    
    /**
	 * Creates the menu, with the menu-items specified in the menu xml file
	 */    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    	getMenuInflater().inflate(R.menu.menu, menu);
	
    	return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

	    	switch(item.getItemId()){
	
		case R.id.itemAddFav:
			
			favmanage.saveFavorite(exam);
			break;
	
		case R.id.itemRemoveFav:
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Do you want to delete this exam?")
			       .setCancelable(false)
			       .setTitle("Confirmation")
			       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			        	   favmanage.deleteFavorite(exam);
			        	   Intent intent = new Intent(); 
			                intent.setClass(getApplicationContext(), FavouriteActivity.class);
			                startActivity(intent);
			                finish();
			           }
			       })
			       .setNegativeButton("No", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			                dialog.cancel();
			           }
			       });
			
			AlertDialog alert = builder.create();
					alert.show();
			break;
			
		case R.id.itemFav:
			startActivity(new Intent(this,FavouriteActivity.class));
			break;
		
		case R.id.itemFind:
			startActivity(new Intent(this, FindActivity.class));
			break;


	
	    	}
	
	
	
			return true;
			}
    
    
    /**
     * This method will prepare the menu with the desired menu-items
     */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		
		super.onPrepareOptionsMenu(menu);
		
		//If the previous activity was an favourite activity
		if(flag){
			//remove favourite option will be available in the menu
			MenuItem item = menu.findItem(R.id.itemRemoveFav);
		    item.setVisible(true);
		    item.setEnabled(true);
			
		}else{
			//if the previous activity was an resultactivity
			//the add favourite option will be available in the menu
			MenuItem item = menu.findItem(R.id.itemAddFav);
		    item.setVisible(true);
		    item.setEnabled(true);
			
			
		}
		
		return true;
	}


	
	

}
